package com.paipai.helper.util;

import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import java.io.DataOutputStream;
import java.io.OutputStream;

import static com.paipai.helper.util.SPConstant.SP_COLLECT_X;
import static com.paipai.helper.util.SPConstant.SP_COLLECT_Y;
import static com.paipai.helper.util.SPConstant.SP_SEED_X;
import static com.paipai.helper.util.SPConstant.SP_SEED_Y;

/**
 * 类描述：
 * <p>
 * 作者：TB.Liu on 19-3-12 17:02
 */
public class CmdUtil {

    public static void collectClick(Context context){
        final int x = SPConstant.getIntDataFromCacheByKey(context, SP_COLLECT_X);
        final int y = SPConstant.getIntDataFromCacheByKey(context, SP_COLLECT_Y);
//        if(x== 0 && y == 0){
//            Toast.makeText(context, "请您先保存点击位置", Toast.LENGTH_SHORT).show();
//            return;
//        }
        Log.d("FloationView", "执行收取命令");
//        String cmd ="input tap "+x +" "+y;
        String cmd ="setprop call.shake null";
        executeCmdForLeiDianSimulater(cmd);
    }

    public static void seedTypeClick(Context context){
        final int x = SPConstant.getIntDataFromCacheByKey(context, SP_COLLECT_X);
        final int y = SPConstant.getIntDataFromCacheByKey(context, SP_COLLECT_Y);
//        if(x== 0 && y == 0){
//            Toast.makeText(context, "请您先保存点击位置", Toast.LENGTH_SHORT).show();
//            return;
//        }
        Log.d("FloationView", "执行选取种子命令");
        String cmd ="input tap "+x +" "+y;
        executeCmd(cmd);
    }

    public static void seedClick(Context context){
        int x = SPConstant.getIntDataFromCacheByKey(context, SP_SEED_X);
        int y = SPConstant.getIntDataFromCacheByKey(context, SP_SEED_Y);
        if(x== 0 && y == 0){
            Toast.makeText(context, "请您先保存点击位置", Toast.LENGTH_SHORT).show();
            return;
        }
        Log.d("FloationView", "执行种植命令");
        String cmd ="input tap "+x +" "+y;
        executeCmd(cmd);
    }

    public static void callBack(){
        Log.d("FloationView", "物理返回键命令");
        String cmd ="input keyevent " + KeyEvent.KEYCODE_BACK;
        executeCmd(cmd);
    }

    public static void getRoot(){
        executeCmd("");
    }

    private static void executeCmdForLeiDianSimulater(String cmd){
        Log.e("FloationView", "执行命令："+cmd);
        try{
            Process process = Runtime.getRuntime().exec("");
            OutputStream outputStream = process.getOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
            dataOutputStream.writeBytes(cmd);
            dataOutputStream.flush();
            dataOutputStream.close();
            outputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void executeCmd(String cmd){
        Log.e("FloationView", "执行命令："+cmd);
        try{
            Process process = Runtime.getRuntime().exec("su");
            OutputStream outputStream = process.getOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
            dataOutputStream.writeBytes(cmd);
            dataOutputStream.flush();
            dataOutputStream.close();
            outputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
